package controller

import (
	"model"
	"strconv"
	"regexp"
	"fmt"
)

var BaseDir = `D:\Go\comment/`
func ReadCommentFile(){
	MoveWhere := model.Moves{
		GetInfo:1,
		IsUnique:1,
	}
	page := 1
	limit := 10
	field := "id,dou_id,move_name"
	for {
		//1.获取电影数据列表
		moves := model.GetMoveList(MoveWhere,page,limit,field)
		//2.验证退出条件
		if len(moves) <= 0 {
			break
		}
		for _,val := range moves {
			File := strconv.Itoa(val.DouId)
			for i:=0;i<=10;i++{
				Path := BaseDir+File+"_"+strconv.Itoa(i)+".html"
				fmt.Printf(Path)
				info,err := GetFileInfo(Path)
				if err != nil {
					panic(err)
				}
				RegxGetHtmlCommentList(val.DouId,info)
			}
			//break
		}
		//break
		page++
	}

}

func RegxGetHtmlCommentList(move_id int,data []byte){
	//1.获取独立的评论模块
	//[1:"豆瓣评论id",2:"用户昵称",3:"豆瓣用户个人主页地址",4:"用户头像",5:"点赞数",6:"用户评分",7:"评论时间",8:"评论内容"]
	str := `<div class="comment-item" data-cid="(\w+)">[^<]*<div[^<]+<a title="(.+)" href="https://www.douban.com/people`
	str += `/(\w+)/">[^<]*<img src="(.+)" class="" />[^<]*</a>[^<]*</div>[^<]*<div [^>]*>[^<]*<h3>[^<]*<span class="comme`
	str += `nt-vote">[^<]*<span class="votes">(\d+)</span>[^<]*<input [^>]*>[^<]*<a[^>]*>[^>]*</a>[^<]*</span>[^<]*<span`
	str += `[^>]*>[^<]*<a[^>]*>[^<]*</a>[^<]*<span>[^<]*</span>[^<]*<span class="allstar(\d+)[^>]*>[^<]*</span>[^<]*<span[^>]* title="(.*)">[^<]*`
	str += `</span>[^<]*</span>[^<]*</h3>[^<]*<p[^>]*>[^<]*<span[^>]*>([^<]*)</span>[^<]*</p>[^<]*</div>[^<]*</div>`
	regx := regexp.MustCompile(str)
	//2.获取单个评论模块种的内容、点赞、用户信息
	//2.1 评论内容
	//regx := regexp.MustCompile(`<span class="short"[^>]*>([^<]+)</span>`)
	match := regx.FindAllSubmatch(data,-1)
	//MemberData := make(map[int]model.Member)
	for _,val := range match{
		var member = model.Member{
			NikeName:string(val[2]),
			HeadImg:string(val[4]),
			MemberFrom:1,
			MemberLink:string(val[3]),
		}
		MemberId := model.InsertMember(member)
		score,_ := strconv.Atoi(string(val[6]))
		likes,_ := strconv.Atoi(string(val[5]))
		var Comments = model.MoveComment{
			MoveId:move_id,
			UserId:MemberId,
			NickName:string(val[2]),
			Content:string(val[8]),
			Score:score,
			Likes:likes,
			Items:"",
			CreateTime:0,
		}
		res := model.InsertComment(Comments)
		fmt.Println(res)
	}
	//2.2 评论点赞数
	//2.3 用户信息


}
